<?php
session_start();
        ini_set('default_charset', 'utf-8');
 		header('Content-Type: text/html; charset=utf-8');
       
        $con=mysql_connect("sql307.freevnn.com",'freev_11941239','khoaluan')or die(mysql_error());
        mysql_select_db("freev_11941239_kfc",$con) or die(mysql_error());	
		mysql_set_charset('utf8',$con);
        mysql_query("SET NAMES 'utf8'",$con);
       
        /*$data = file_get_contents('php://input');		
        $data = stripcslashes($data);
        if($data !=null)
        {
            $json= json_decode($data);
            if($json===null)
            {
                echo "khong decode duoc json";
            }
            else
            {
                echo "<pre>";
                    print_r($json);
                echo "</pre>";
                
                foreach($json->product as $value)
                {
                    $name = $value->name;
                    $number = $value->number;
                    $sql = "INSERT INTO order_food(name,number) VALUES ('$name','$number')";
                    echo $sql;
                    mysql_query($sql);
                }
            }
        }
        
        $sql1 = "SELECT * FROM menu,order_food WHERE menu.name=order_food.name";
        $query = mysql_query($sql1);
        while($data = mysql_fetch_assoc($query))
        {
            echo $data["name"];
            echo " = ";
            echo $data["price"];
        }

        
        
       // echo $json["name"];
        /*if(get_magic_quotes_gpc())
        {
            $data = stripslashes($data);
        }
        $data1 = mysql_real_escape_string($data);
        
        $con=mysql_connect("sql307.freevnn.com",'freev_11941239','khoaluan')or die(mysql_error());
        mysql_select_db("freev_11941239_kfc",$con) or die(mysql_error());	
        mysql_query("SET NAMES 'utf8'",$con);
        mysql_set_charset('utf8',$con);
        if($data!=null)
        {
            $sql = "INSERT INTO recevie_food(name) VALUES('$data1')";
            mysql_query($sql);
        }*/
        
	if(!isset($_GET['check'])){		
		$data=file_get_contents('php://input');
        //if($data!=null)
        //{
            
            $data = stripcslashes($data);
			$decode=json_decode($data);	           
			date_default_timezone_set("Asia/Ho_Chi_Minh");
			$date=date('Y-m-d H:i:s');
			$table=$decode->table;		
			foreach($decode->product as $value){
			         if(!empty($value->name))
                     {
    					$sll=$value->number;
    					$mid=$value->name;
    					//$table=$value->table;
    					$sql="insert into detail_order(menu_name,table_id,date_order,SLL) values('$mid','$table','$date','$sll')";		
    					mysql_query($sql);		
                    }		
					
			}
			//$_SESSION['ses_table']=$table;
       //}
		
	
	}else if(isset($_GET['check']) && $_GET['check']==2){
		$id=$_GET['id'];
		$butt=0;
		date_default_timezone_set("Asia/Ho_Chi_Minh");
		$date=date('Y-m-d H:i:s');
		$_SESSION['ses_date']=$date;
		$_SESSION['ses_current']=$_SESSION['ses_table'];
		if(isset($_SESSION['ses_table'])){			
			unset($_SESSION['ses_table']);
		}
			//date_order=(select max(date_order) from detail_order where table_id=$id)
			$sqltest="select date_order,date_complete from detail_order where date_order=(select max(date_order) from detail_order where table_id=$id) and table_id=$id";
			$querytest=mysql_query($sqltest);
			$data=mysql_fetch_assoc($querytest);
			if(strtotime($data["date_order"])>strtotime($data["date_complete"])){
				$sql="select * from detail_order where checked_status='N' and table_id=$id";
			}else{
				$sql="select * from detail_order where date_complete=(select max(date_complete) from detail_order where table_id=$id) and table_id=$id";
				$butt=1;
			}
			$query=mysql_query($sql);
			$total=0;
			echo "<div id='detail_order'>";
			echo "<p id='info_menu'>Thực đơn bàn số <span>$id</span></p>";
			echo "<div class='title'>
					<div class='name_food'><p>Tên món ăn</p></div>
					<div class='price_food'><p>Giá tiền:</p></div>
					<div class='sll'><p>Số lượng</p></div>
				</div>";
			while($data=mysql_fetch_assoc($query)){
				$mid=$data['menu_name'];			
				$sql2="select * from menu where name='$mid'";
				$query2=mysql_query($sql2);			
				$data2=mysql_fetch_assoc($query2);						
				$price=number_format($data2['price'],0,'.',',');
				$total=$total+$data2['price']*$data['SLL'];
				echo "<div class='food'>
						<div class='name_food'><p>$data2[name]</p></div>
						<div class='price_food'><p>$price</p></div>
						<div class='sll'><p>$data[SLL]</p></div>
					</div>";
			}
			$total=number_format($total,0,'.',',');
			echo "<div class='sum'>
					<div class='name_food'><p>Total:</p></div>
					<div class='total'><p>$total</p></div>";			
			echo "</div>";
			if($butt==0){
				echo "<button id='cook_now'>Thực hiện</button>";
			}else{
				echo "<button id='cook_now' disabled='disabled'>Đã thực hiện</button>";
			}
			echo "</div>";
			//$sql3="update detail_order set checked='y' where table_id=$id";
			//mysql_query($sql3);
		
	}else if(isset($_GET['check']) && $_GET['check']==3){
		$id=$_GET['id'];
		date_default_timezone_set("Asia/Ho_Chi_Minh");		
		$date=date('Y-m-d H:i:s');		
		$sql="update detail_order set checked_status='Y',date_complete='$date' where table_id='$id' and checked_status='N'";
		mysql_query($sql);
		//$_SESSION['ses_tabletocook']=$id;
		//$_SESSION['ses_tableclicked'][$id]=0;
						
	}else if(isset($_GET['check']) && $_GET['check']==1){
		$sql="select table_id,date_order from detail_order where date_order=(select max(date_order) from detail_order where checked_status='N') and checked_status='N'";
		$query=mysql_query($sql);
		if(mysql_num_rows($query)>0){		
			$data=mysql_fetch_assoc($query);
			$_SESSION['ses_table']=$data['table_id'];
			if($_SESSION['ses_current']!=$_SESSION['ses_table']){			
				echo $_SESSION['ses_table'];			
			}else{
					 if(strtotime($_SESSION['ses_date'])<strtotime($data['date_order'])){
						echo $_SESSION['ses_table'];
					 }
					else{
						echo 0;
					 }			
			}		
		}else{
			echo 0;
		}
	}else if(isset($_GET['check']) && $_GET['check']==5){
		/*if(isset($_SESSION['ses_tabletocook']))
			echo $_SESSION['ses_tabletocook'];
		else
			echo 0;*/
		$sql="select table_id,date_complete from detail_order where date_complete=(select max(date_complete) from detail_order where checked_status='Y') and checked_status='Y'";
		$query=mysql_query($sql);
		if(mysql_num_rows($query)>0){
			$data=mysql_fetch_assoc($query);
			$id=$data['table_id'];
			$_SESSION['ses_tabletocook']=$data['table_id'];			
			if($_SESSION['ses_currenttocook']!=$_SESSION['ses_tabletocook']){			
				echo $_SESSION['ses_tabletocook'];
				$_SESSION['ses_tableclicked'][$id]=0;			
			}else{
					 if(strtotime($_SESSION['ses_datetocook'])<strtotime($data['date_complete'])){
						echo $_SESSION['ses_tabletocook'];
						$_SESSION['ses_tableclicked'][$id]=0;
					 }
					 else{
						echo 0;
					 }			
			}		
		}else{
			echo 0;			
		}
		
	}else if(isset($_GET['check']) && $_GET['check']==6){
		$id=$_GET['id'];
		$butt=0;
		date_default_timezone_set("Asia/Ho_Chi_Minh");
		$date=date('Y-m-d H:i:s');
		$_SESSION['ses_datetocook']=$date;
		$_SESSION['ses_currenttocook']=$_SESSION['ses_tabletocook'];
		if(isset($_SESSION['ses_tabletocook'])){
			unset($_SESSION['ses_tabletocook']);
		}		
		if($_SESSION['ses_tableclicked'][$id]==1||!isset($_SESSION['ses_tableclicked'][$id])){
			$butt=1;
		}
			$sql="select * from detail_order where table_id=$id and checked_status='Y' and cook_status='N'";
			$query=mysql_query($sql);
			$total=0;
			echo "<div id='detail_order'>";
			echo "<p id='info_menu'>Thực đơn bàn số <span>$id</span></p>";
			echo "<div class='title'>
					<div class='name_food'><p>Tên món ăn</p></div>
					<div class='price_food'><p>Giá tiền:</p></div>
					<div class='sll'><p>Số lượng</p></div>
				</div>";
			while($data=mysql_fetch_assoc($query)){
				$mid=$data['menu_name'];			
				$sql2="select * from menu where name='$mid'";
				$query2=mysql_query($sql2);			
				$data2=mysql_fetch_assoc($query2);						
				$price=number_format($data2['price'],0,'.',',');
				$total=$total+$data2['price']*$data['SLL'];
				echo "<div class='food'>
						<div class='name_food'><p>$data2[name]</p></div>
						<div class='price_food'><p>$price</p></div>
						<div class='sll'><p>$data[SLL]</p></div>
					</div>";
			}
			$total=number_format($total,0,'.',',');
			echo "<div class='sum'>
					<div class='name_food'><p>Total:</p></div>
					<div class='total'><p>$total</p></div>";			
			echo "</div>";
			if($butt==0){
				echo "<button id='cook_completed'>Đã nấu xong</button>";
			}else{
				echo "<button id='cook_completed' disabled='disabled'>Đã nấu xong</button>";
			}
			echo "</div>";	
	}else if(isset($_GET['check']) && $_GET['check']==7){
		$id=$_GET['id'];
		date_default_timezone_set("Asia/Ho_Chi_Minh");		
		$date=date('Y-m-d H:i:s');		
		$_SESSION['ses_tableclicked'][$id]=1;
		$sql="update detail_order set cook_status='Y' where table_id='$id' and cook_status='N' and checked_status='Y'";
		mysql_query($sql);
		//$sql1="update table_completed set name_table='$id',date_cook_completed='$date'";
		//mysql_query($sql1);
	
	}else if(isset($_GET['check']) && $_GET['check']==8){
		$sql="select table_id,date_complete from detail_order where date_complete=(select max(date_complete) from detail_order where cook_status='Y' and checked_status='Y') and cook_status='Y' and checked_status='Y'";
		$query=mysql_query($sql);
		while($data=mysql_fetch_assoc($query)){		
			if($_SESSION['ses_dcc']!=$data['date_complete']){
				$_SESSION['ses_dcc']=$data['date_complete'];
				echo "Bàn số <span id='ban'>$data[table_id]</span> đã nấu xong thực đơn gửi lúc: <span id='thoi_gian'>$data[date_complete]</span><br/>";
			}		
		}
	}
	
?>